#lang racket

;TREE SELECTORS AND CONSTRUCTORS-----------------------------------------------------------------------------

(define (entry tree) (car tree))

(define (left-branch tree) (cadr tree))

(define (right-branch tree) (caddr tree))

(define (make-tree entry left right)
  (list entry left right))

;HELP PROCEDURES---------------------------------------------------------------------------------------------

(define (tree->list tree)
  (define (copy-to-list tree result-list)
    (if (null? tree)
        result-list
        (copy-to-list (left-branch tree)
                      (cons (entry tree)
                            (copy-to-list (right-branch tree)
                                          result-list)))))
  (copy-to-list tree '()))

(define (list->tree elements)
  (car (partial-tree elements (length elements))))

(define (partial-tree elts n)
  (if (= n 0)
      (cons '() elts)
      (let ((left-size (quotient (- n 1) 2)))
        (let ((left-result (partial-tree elts left-size)))
          (let ((left-tree (car left-result))
                (non-left-elts (cdr left-result))
                (right-size (- n (+ left-size 1))))
            (let ((this-entry (car non-left-elts))
                  (right-result (partial-tree (cdr non-left-elts)
                                              right-size)))
              (let ((right-tree (car right-result))
                    (remaining-elts (cdr right-result)))
                (cons (make-tree this-entry left-tree right-tree)
                      remaining-elts))))))))

;SET OPERATIONS (SORTED LISTS)-------------------------------------------------------------------------------

(define (sl-element-of-set? x set)
  (cond ((null? set) false)
        ((= x (car set)) true)
        ((< x (car set)) false)
        (else (sl-element-of-set? x (cdr set)))))

(define (sl-intersection-set set1 set2)
  (if (or (null? set1) (null? set2))
      '()
      (let ((x1 (car set1))
            (x2 (car set2)))
        (cond ((= x1 x2)
               (cons x1
                     (sl-intersection-set (cdr set1)
                                          (cdr set2))))
              ((< x1 x2)
               (sl-intersection-set (cdr set1) set2))
              ((< x2 x1)
               (sl-intersection-set set1 (cdr set2)))))))

(define (sl-adjoin-set x set)
  (cond ((null? set) (list x))
        ((= x (car set)) set)
        ((< x (car set)) (cons x set))
        (else (cons (car set) (sl-adjoin-set x (cdr set))))))

(define (sl-union-set set1 set2)
  (cond ((null? set1) set2)
        ((null? set2) set1)
        (else (let ((x1 (car set1))
                    (x2 (car set2)))
                (cond ((= x1 x2) (cons x1 
                                       (sl-union-set (cdr set1)
                                                     (cdr set2))))
                      ((< x1 x2) (cons x1
                                       (sl-union-set (cdr set1)
                                                     set2)))
                      ((< x2 x1) (cons x2
                                       (sl-union-set set1 
                                                     (cdr set2)))))))))

;SET OPERATIONS (TREES)--------------------------------------------------------------------------------------

(define (adjoin-set x set)
  (cond ((null? set) (make-tree x '() '()))
        ((= x (entry set)) set)
        ((< x (entry set))
         (make-tree (entry set)
                    (adjoin-set x (left-branch set))
                    (right-branch set)))
        ((> x (entry set))
         (make-tree (entry set)
                    (left-branch set)
                    (adjoin-set x (right-branch set))))))

(define (element-of-set? x set)
  (cond ((null? set) false)
        ((= x (entry set)) true)
        ((< x (entry set))
         (element-of-set? x (left-branch set)))
        ((> x (entry set))
         (element-of-set? x (right-branch set)))))

(define (union-set set1 set2)
  (let ((set-as-list1 (tree->list set1))
        (set-as-list2 (tree->list set2)))
    (let ((union-set-as-list (sl-union-set set-as-list1 set-as-list2)))
      (list->tree union-set-as-list))))

(define (intersection-set set1 set2)
  (let ((set-as-list1 (tree->list set1))
        (set-as-list2 (tree->list set2)))
    (let ((intersection-set-as-list (sl-intersection-set set-as-list1 set-as-list2)))
      (list->tree intersection-set-as-list))))

;TESTING-----------------------------------------------------------------------------------------------------

(define set-as-list1 '(1 3 4 5 6 7 9))

(define set-as-list2 '(2 4 5 6 7 8 10))

(define set1 (list->tree set-as-list1))

(define set2 (list->tree set-as-list2))

(union-set set1 set2)

(intersection-set set1 set2)